package org.sky.generator.generator.impl;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.sky.generator.GeneratorConfig;
import org.sky.generator.config.MapperGeneratorConfig;
import org.sky.generator.database.model.Column;
import org.sky.generator.database.model.Table;
import org.sky.generator.freemarker.FtlParam;
import org.sky.generator.generator.IGenerator;
import org.sky.util.LogUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.List;

/**
 * @author weifx
 * @Title: MapperGenerator
 * @ProjectName springboot-platform
 * @Description: TODO{Mapper接口代码生成器}
 * @date 2019/2/27 11:13
 * @Version 1.0
 */
public class MapperGenerator implements IGenerator {
    private static Logger logger = LogUtils.getLogger(MapperGenerator.class);
    private static MapperGeneratorConfig mapperGeneratorConfig;
    private static final String templateDir = "/template";
    private static final String templateFile = "mapper.ftl";
    @Override
    public void generate(GeneratorConfig generatorConfig, Table table, List<Column> columnList) {
        mapperGeneratorConfig = generatorConfig.getMapperGeneratorConfig();
        Writer out = null;
        try {
            Configuration cfg = new Configuration(Configuration.getVersion());
            cfg.setDirectoryForTemplateLoading(new File(MapperGenerator.class.getResource(templateDir).getFile()));
            cfg.setObjectWrapper(new DefaultObjectWrapper(Configuration.getVersion()));
            Template tpl = cfg.getTemplate(templateFile);

            if (table != null) {
                if (!StringUtils.isBlank(table.getTableName())) {
                    String targetDir = mapperGeneratorConfig.getTargetProject()+File.separator
                            + mapperGeneratorConfig.getTargetPackage().replace(".",File.separator);
                    String mapperName = table.getEntityName()+"Mapper";
                    String targetFileName = targetDir +File.separator+ mapperName +".java";
                    File outFile = new File(targetFileName);
                    if(!outFile.getParentFile().exists()){
                        outFile.getParentFile().mkdirs();
                    }
                    out = new FileWriter(outFile);
                    if(!outFile.getParentFile().exists()){
                        outFile.getParentFile().mkdirs();
                    }
                    table.setColumnList(columnList);
                    FtlParam ftlParam = new FtlParam().setGeneratorConfig(generatorConfig).setTable(table);
                    tpl.process(ftlParam, out);
                    logger.info("Mapper:" +mapperName+ ".java 代码生成成功,绝对路径:{}",outFile.getAbsolutePath());
                }
            }
        }catch (Exception e){
            logger.error("Mapper生成错误",e);
        }finally {
            if (null!=out) {
                try {
                    out.flush();
                    out.close();
                }catch (Exception e){
                    logger.error("out关闭错误",e);
                }
            }
        }
    }
}
